<template>
<div>
  <!-- 面包屑导航 -->
  <el-breadcrumb separator-class="el-icon-arrow-right">
    <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
    <el-breadcrumb-item>试题管理</el-breadcrumb-item>
    <el-breadcrumb-item>所有试题</el-breadcrumb-item>
  </el-breadcrumb>

  <!--试题列表主体部分-->
  <el-card>
    <el-row :gutter="25">
      <!--span为所占区域比例-->
      <el-col :span="10">
        <!-- 搜索添加 -->
        <el-input placeholder="请输入需要搜素的题目内容..." v-model="queryInfo.query" clearable @clear="getProblemList">
          <el-button slot="append" icon="el-icon-search" @click="getProblemList"></el-button>
        </el-input>
      </el-col>
      <el-col :span="4">
        <el-button type="primary" @click="Toaddpaper">添加试题</el-button>
      </el-col>
    </el-row>
  <el-table :data="problemlist" style="width: 100%"  height="468" :cell-style="cellStyle" :header-cell-style="rowClass">
    <el-table-column type="index"></el-table-column>
    <el-table-column label="试题编号" width="100px" prop ="pro_no"></el-table-column>
<!--    <el-table-column label="试题内容" width="130px" prop="pro_detail"></el-table-column>-->
    <el-table-column label="答案编号" width="80px"  prop="pro_ansno"></el-table-column>
<!--    <el-table-column label="详细答案" width="150px" prop="cha_mpoint"></el-table-column>-->
    <el-table-column label="相关知识" width="150px" prop="pro_keyw"></el-table-column>
    <el-table-column label="难度系数" width="100px" prop="pro_dif"></el-table-column>
    <el-table-column label="试题分数" width="100px" prop="cha_no"></el-table-column>
    <el-table-column label="章节标题" width="160px" prop="cha_title"></el-table-column>
    <el-table-column label="试题专业代码" width="120px" prop="pro_maj"></el-table-column>

    <el-table-column label="操作" min-width="120px" fixed="right">
      <template slot-scope="scope" width="150px">
        <!-- 修改 -->
        <el-button type="primary" icon="el-icon-edit" size="mini" @click="showEditDialog(scope.row.pro_no)"></el-button>
        <!-- 删除 -->
        <el-button type="danger" icon="el-icon-delete" size="mini" @click="deleteProblem(scope.row.pro_no)"></el-button>
        <!--试题详情-->
        <el-button type="success"  size="mini" @click="showdetailDialog(scope.row.pro_no)" >试题详情</el-button>
      </template>
    </el-table-column>
  </el-table>

<!--分页功能-->
    <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pageNum"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="queryInfo.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
    </el-pagination>
  </el-card>

  <!-- 修改试题对话框 -->
  <el-dialog title="修改试题信息" :visible.sync="editDialogVisible" width="50%" @colse="editDialogClosed">
    <el-form :model="editForm" :rules="editFormRules" ref="editFormRef" label-width="90px">
      <el-form-item label="试题内容" prop="pro_detail">
        <el-input type="textarea" rows="4"  v-model="editForm.pro_detail" ></el-input>
      </el-form-item>
      <el-form-item label="答案编号" prop="pro_ansno">
        <el-input v-model="editForm.pro_ansno"></el-input>
      </el-form-item>
      <el-form-item label="详细答案" prop="cha_mpoint">
        <el-input type="textarea" rows="4"  v-model="editForm.cha_mpoint"></el-input>
      </el-form-item>
      <el-form-item label="相关知识" prop="pro_keyw">
        <el-input v-model="editForm.pro_keyw"></el-input>
      </el-form-item>
      <el-form-item label="难度系数" prop="pro_dif">
        <el-input v-model="editForm.pro_dif"></el-input>
      </el-form-item>
      <el-form-item label="试题分数" prop="cha_no">
        <el-input v-model="editForm.cha_no"></el-input>
      </el-form-item>
      <el-form-item label="章节标题" prop="cha_title">
        <el-input v-model="editForm.cha_title"></el-input>
      </el-form-item>
      <el-form-item label="专业代码" prop="pro_maj">
        <el-input v-model="editForm.pro_maj"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editProblemInfo">确 定</el-button>
      </span>
  </el-dialog>
<!--详情窗口-->
  <el-dialog title="试题详情信息" :visible.sync="detailDialogVisible" >
    <el-form :model="editForm"  ref="editFormRef" label-width="70px">
      <el-form-item label="试题内容" prop="pro_detail">
        <el-input :disabled="true" type="textarea" rows="4"  v-model="editForm.pro_detail" ></el-input>
      </el-form-item>
      <el-form-item label="详细答案" prop="cha_mpoint">
        <el-input :disabled="true" type="textarea" rows="4"  v-model="editForm.cha_mpoint"></el-input>
      </el-form-item>
    </el-form>
  </el-dialog>
 </div>
</template>

<script>
export default {
  created() {
    this.getProblemList();
  },
  data(){
    return {
      queryInfo: {
        query:"",
        pageNum: 1,
        pageSize: 10
      },
      problemlist:[],
      total: 0,
      editDialogVisible:false,
      detailDialogVisible:false,
      editForm:{},
      editFormRules: {},
      editFormRules:{
        pro_detail:[
          {required: true, message: "请输入正确的题目！", trigger: "blur"},
          {min: 1, max: 1000, message: "内容不能为空！", trigger: "blur"}
        ],
        pro_ansno:[
          {required: true, message: "请输入唯一答案标识", trigger: "blur"},
          {min: 2, max: 15, message: "长度在 2 到 15 个字符", trigger: "blur"}
        ],
        pro_maj:[
          {required: true, message: "请输入试卷所属专业代码", trigger: "blur"},
          {min: 2, max: 15, message: "请输入数字编号", trigger: "blur"}
        ],
        cha_mpoint:[
          {required: true, message: "请输入试题解答过程", trigger: "blur"},
          {min: 5, max: 1000, message: "不少于5个字符", trigger: "blur"}
        ],
        cha_no:[
          {required: true, message: "请输入试题分数", trigger: "blur"},
          {min: 1, max: 15, message: "填数字且不少于2个字符", trigger: "blur"}
        ],
        cha_title:[
          {required: true, message: "请输入章节标题", trigger: "blur"},
          {min: 2, max: 15, message: "请输入正确章节标题", trigger: "blur"}
        ],
        pro_dif:[
          {required: true, message: "请输入数字", trigger: "blur"},
          {min: 1,  message: "等级在1~5之间", trigger: "blur"}
        ],
        pro_keyw:[
          {required: true, message: "请输入相关知识", trigger: "blur"},
          {min: 1,  message: "请输入相关知识", trigger: "blur"}
        ],
      },
    };
  },
  methods:{
    //数据居中
   cellStyle({row,column,rowIndex,columnIndex}){
      return "text-align:center";
    },
     rowClass({row,rowIndex}){
     return "text-align:center";
    },
    //跳转添加试题
    Toaddpaper() {
      window.sessionStorage.clear();
      this.$router.push("/addpaper");
    },

  //  获取试题数据
    async getProblemList(){
      console.log(123);
      const {data:res } = await this.$http.get("allProblem",{params:this.queryInfo});
      this.problemlist = res.data;
      this.total = res.numbers;
    },
    handleSizeChange(newSize){
      this.queryInfo.pageSize = newSize;
      this.getProblemList();
    },
    handleCurrentChange(newPage) {
      this.queryInfo.pageNum = newPage;
      this.getProblemList(); // 数据发生改变重新申请数据
    },

    // 展示修改框
    async showEditDialog(pro_no){
      const {data:res} = await this.$http.get("getUpdateProblem?pro_no=" + pro_no);
      this.editForm = res;
      this.editDialogVisible = true;
    },
    // 展示详情框
      async showdetailDialog(pro_no){
      const {data:res} = await this.$http.get("getUpdateProblem?pro_no=" + pro_no);
        this.editForm = res;
        this.detailDialogVisible = true;
    },
    // 关闭窗口
    editDialogClosed(){
      this.$refs.editFormRef.resetFields();
    },
    // 确认修改
    editProblemInfo(){
      this.$refs.editFormRef.validate(async valid =>{
        console.log(valid);
        if( !valid ) return;
        // 发起请求
        const {data:res} = await this.$http.put("editProblem",this.editForm);
        console.log(res);
        if (res != "success") return this.$message.error("操作失败！！！");
        this.$message.success("操作成功！！！");
        //隐藏
        this.editDialogVisible = false;
        this.getProblemList();
      });
    },
    // 删除按钮
    async deleteProblem(pro_no){
      // 弹框
      const confirmResult = await this.$confirm('此操作将从题库中删除该题目, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      // 成功删除为confirm 取消为 cancel
      if(confirmResult!='confirm'){
        return this.$message.info("已取消删除");
      }
      const {data:res} = await this.$http.delete("deleteProblem?pro_no="+pro_no);
      if (res != "success") {
        return this.$message.error("操作失败！！！");
      }
      this.$message.success("操作成功！！！");
      this.getProblemList();
    },
  },
}
</script>

<style  lang="less" scoped>
.el-breadcrumb{
  margin-bottom: 15px;
  font-size: 17px;
}

</style>